package izzat.fit.dbservice.controller;

import izzat.fit.dbservice.entity.RequestEntity;
import izzat.fit.dbservice.entity.ResponseEntity;
import izzat.fit.dbservice.entity.mine.HistoricalStatistics;
import izzat.fit.dbservice.entity.mine.Record;
import izzat.fit.dbservice.entity.mine.Schedule;
import izzat.fit.dbservice.service.MineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MineDBServiceController
 * @Description:
 * @Author: Izzat
 * @Date: 2020-01-31 00:05
 * @Version: v1.0
 */
@RestController
@RequestMapping("db-service/mine")
public class MineDBServiceController {
    @Autowired
    private MineService mineService;
    
    @RequestMapping("findScheduleByUserId")
    /** 
    * @Description: 按用户名查找安排计划
    * @Param: [id] 
    * @return: izzat.fit.dbservice.entity.ResponseEntity 
    * @Author: Izzat 
    * @Date: 2020/1/31  0:50
    */ 
    public ResponseEntity findScheduleByUserId(@RequestBody int request){
        ResponseEntity response = new ResponseEntity();
        List<Schedule> serchResult = mineService.getScheduleByUserId(request);
        String info;
        int size = serchResult.size();
        if(size > 0){
            info = "查询成功，为您找到" + size + "条记录";
            response.status(200).datas(serchResult).info(info);
        }else {
            info = "找不到相关记录";
            response.status(400).info(info);
        }
        return response;
    }

    @RequestMapping("updatePlanByUserAndDate")
    /** 
    * @Description: 更新训练计划 
    * @Param: [request] 
    * @return: izzat.fit.dbservice.entity.ResponseEntity
    * @Author: Izzat 
    * @Date: 2020/2/4  14:36
    */ 
    public ResponseEntity updatePlanByUserAndDate(@RequestBody Schedule request){
        ResponseEntity response = new ResponseEntity();
        int updateResult = mineService.updatePlanByUserAndDate(request);
        String info;
        if(updateResult == 1){
            info = "更新成功，已为您修改" + updateResult + "条记录";
            response.status(200).datas(new ArrayList<>()).info(info);
        }else {
            info = "更新失败";
            response.status(400).info(info);
        }
        return response;
    }
    
    @RequestMapping("addRecord")
    /** 
    * @Description: 新增训练记录 
    * @Param: [request] 
    * @return: izzat.fit.dbservice.entity.ResponseEntity 
    * @Author: Izzat 
    * @Date: 2020/3/17  10:15
    */ 
    public ResponseEntity addRecord(@RequestBody Record request){
        ResponseEntity response = new ResponseEntity();
        int updateResult = mineService.addRecord(request);
        String info;
        if(updateResult == 1){
            info = "插入成功，已为您插入" + updateResult + "条记录";
            response.status(200).datas(new ArrayList<>()).info(info);
        }else {
            info = "插入失败";
            response.status(400).info(info);
        }
        return response;
    }

    @RequestMapping("findHistory")
    /**
    * @Description: 按照用户id和时间范围查找训练记录
    * @Param: [request]
    * @return: izzat.fit.dbservice.entity.ResponseEntity
    * @Author: Izzat
    * @Date: 2020/3/17  10:42
    */
    public ResponseEntity findHistory(@RequestBody RequestEntity request){
        ResponseEntity response = new ResponseEntity();
        HistoricalStatistics SearchResult = mineService.findRecordByUserAndRange(request);
        List<HistoricalStatistics> list = new ArrayList<>();
        list.add(SearchResult);
        String info;
        if(SearchResult.Null()){
            info = "检索成功，已为您检索到" + SearchResult.getList().size() + "条记录";
            response.status(200).datas(list).info(info);
        }else {
            info = "检索失败";
            response.status(400).info(info);
        }
        return response;
    }
}
